package mall.demo.mybatis.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import mall.demo.mybatis.pojo.User;
import mall.demo.mybatis.util.MybatisSessionUtil;

public class UserService extends BaseService {
	private String userMapper = "mall.demo.mybatis.mapper.UserMapper.";
	private String addNewUser = "addNewUser";
	private String queryByLoginNamePassword = "queryByLoginNamePassword";
	private String selectAll = "selectAll";

	public UserService() {
		// TODO Auto-generated constructor stub
	}
	
	public void addNewUser(User user) {
		SqlSession session = null;
		try {
			session = MybatisSessionUtil.getInstance().openSession();
			String st = this.userMapper + this.addNewUser;
			session.insert(st, user);
			session.commit();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			closeSession(session);
		}
	}
	
	public User getByLoginNamePassword(User user) {
		SqlSession session = null;
		User retUser = null;
		try {
			session = MybatisSessionUtil.getInstance().openSession();
			String st = this.userMapper + this.queryByLoginNamePassword;
			retUser = session.selectOne(st, user);
			return retUser;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			closeSession(session);
		}
	}
	
	public List<User> getAllUsers() {
		SqlSession session = null;
		List<User> retUser = new ArrayList<User>();
		try {
			session = MybatisSessionUtil.getInstance().openSession();
			String st = this.userMapper + this.selectAll;
			retUser = session.selectList(st);
			return retUser;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			closeSession(session);
		}
	}

}
